package com.graduation.rescuematerial.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduation.rescuematerial.exception.GlobalException;
import com.graduation.rescuematerial.mapper.CompanyMapper;
import com.graduation.rescuematerial.pojo.Company;
import com.graduation.rescuematerial.service.CompanyService;
import com.graduation.rescuematerial.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author CaiShuangLian
 * @since 2022-04-11
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {

    @Autowired
    private CompanyMapper companyMapper;

    /**
     * 根据不同类型返回不同的部门列表（department，warehouse，all）
     * @param type
     * @return
     */
    @Override
    public List<CommonCompanyVo> getAllCompany(String type) {
        if(type.equals("all")){
            return companyMapper.getAll();
        }
        return companyMapper.getCompanyByType(type);
    }

    @Override
    public RespBean getWarehouse(WarehouseInfoVo warehouseInfoVo) {
        return RespBean.success(companyMapper.getWarehouse(warehouseInfoVo));
    }

    @Override
    public RespBean registerDepartment(RegisterCompanyVo registerCompanyVo) {

        //判断部门是否注册过
        Company flag=companyMapper.getCompanyByName(registerCompanyVo.getName());
        if(null!=flag){
            return RespBean.error(RespBeanEnum.COMPANY_REPEAT);
        }


        Company company=new Company();
        company.setName(registerCompanyVo.getName());
        company.setAddress(registerCompanyVo.getAddress());
        company.setDescription(registerCompanyVo.getDescription());
        company.setLeaderPhone(registerCompanyVo.getLeaderPhone());
        company.setTelephone(registerCompanyVo.getTelephone());
        company.setHead(registerCompanyVo.getHead());
        company.setLongitude(registerCompanyVo.getLongitude());
        company.setLatitude(registerCompanyVo.getLatitude());
        company.setType(registerCompanyVo.getType());
        if(!registerCompanyVo.getType().equals("warehouse")&&!registerCompanyVo.getType().equals("department")){
            return RespBean.error(RespBeanEnum.BIN_ERROR,"类型错误，只能创建仓库或者单位");
        }

        try {
            if (companyMapper.addOne(company)<1) {
               return RespBean.error(RespBeanEnum.UPDATE_ERROR,"创建失败");
            }
        } catch (Exception e) {
            throw new GlobalException(RespBeanEnum.BIN_ERROR);
        }
        return RespBean.success("注册成功！");

    }

    @Override
    public RespBean getSysCompany() {
        return RespBean.success(companyMapper.getSysCompany());
    }

    @Override
    public RespBean updateCompany(UpdateCompanyVo updateCompanyVo) {
        int row = companyMapper.updateCompany(updateCompanyVo);
        if(row<1){
            return RespBean.error(RespBeanEnum.UPDATE_ERROR,"部门信息提交失败！请检查数据输入是否规范");
        }
        return RespBean.success();
    }

    @Override
    public RespBean getCompanyByName(String companyName) {
        return RespBean.success(companyMapper.getCompanyByName(companyName));
    }

    @Override
    public RespBean getAllCompanyByType(String type) {
        return RespBean.success(companyMapper.getAllCompanyByType(type));
    }

    @Override
    public RespBean unionQuery(CompanyUnionQueryVo companyUnionQueryVo) {
        return RespBean.success(companyMapper.unionQuery(companyUnionQueryVo));
    }

    @Override
    public RespBean companyDropDownBox() {
        return RespBean.success(companyMapper.companyDropDownBox());
    }

    @Override
    public RespBean getUserStatistics() {
        return RespBean.success(companyMapper.getUserStatistics());
    }
}
